package com.liuxm.es.test;

import cn.hutool.json.JSONUtil;
import com.liuxm.es.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class DoclumentOprate {

    RestHighLevelClient client;

    /**
     * 初始化连接
     */
    @Before
    public void before() {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.31.11", 9200, "http")
                )
        );
    }

    /**
     * 断开连接
     */
    @After
    public void after() throws IOException {
        client.close();
    }


    /**
     * 创建文档
     */
    @Test
    public void createDocument() throws IOException {
        IndexRequest request = new IndexRequest("user");
        User user = new User(1L, "鲁班", 18, "1", "鲁班在河道唱着歌");
        request.id(user.getId().toString());
        String jsonStr = JSONUtil.toJsonStr(user);
        request.source(jsonStr, XContentType.JSON);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println("创建文档的响应结果:" + response.getResult());

    }

    /**
     * 查询文档
     */
    @Test
    public void getDocumentById() throws IOException {
        GetRequest request = new GetRequest("user", "1");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        User user = JSONUtil.toBean(response.getSourceAsString(), User.class);
        System.out.println("查询结果：" + user);
    }

    /**
     * 修改文档
     */
    @Test
    public void editDocument() throws IOException {

        User user = getUserById("1");
        user.setName("鲁班");
        UpdateRequest request = new UpdateRequest("user", user.getId().toString());
        request.doc(JSONUtil.toJsonStr(user), XContentType.JSON);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("更新后的结果：" + response.getResult());
    }

    private User getUserById(String id) throws IOException {
        GetRequest request = new GetRequest("user", "1");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        return JSONUtil.toBean(response.getSourceAsString(), User.class);
    }

    /**
     * 删除文档
     */
    @Test
    public void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("user", "1");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println("删除文档的结果：" + response.getResult());
    }

    /**
     * 文档存在
     */
    @Test
    public void exists() throws IOException {
        GetRequest request = new GetRequest("user", "1");
        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println("文档是否存在:" + exists);
    }

    /**
     * 批量添加文档
     */
    @Test
    public void bulkList() throws IOException {
        // 需要导入的集合数据
        List<User> userList = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱")
        );

        BulkRequest request = new BulkRequest("user");
        for (User user : userList) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(user.getId().toString());
            indexRequest.source(JSONUtil.toJsonStr(user), XContentType.JSON);
            request.add(indexRequest);
        }
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.status());
    }

}
